package com.yzh.openai.domain.openai.service;

import com.alibaba.fastjson.JSON;
import com.yzh.openai.domain.openai.model.aggregates.ChatProcessAggregate;
import com.yzh.openai.domain.openai.model.entity.MessageEntity;
import com.yzh.openai.domain.openai.model.entity.RuleLogicEntity;
import com.yzh.openai.domain.openai.model.entity.UserAccountEntity;
import com.yzh.openai.domain.openai.model.valobj.LogicCheckTypeVO;
import com.yzh.openai.domain.openai.service.rule.ILogicFilter;
import com.yzh.openai.domain.openai.service.rule.factory.DefaultLogicFactory;
import com.yzh.zhipu.model.ChatRequest;
import com.yzh.zhipu.model.ChatResponse;
import com.yzh.zhipu.model.args.Prompt;
import com.yzh.zhipu.model.vo.EventType;
import com.yzh.zhipu.model.vo.Model;
import okhttp3.sse.EventSource;
import okhttp3.sse.EventSourceListener;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.springframework.stereotype.Service;
import org.springframework.web.servlet.mvc.method.annotation.ResponseBodyEmitter;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ExecutionException;
import java.util.stream.Collectors;

@Service
public class ChatService extends AbstractChatService {
    @Resource
    private DefaultLogicFactory logicFactory;

    @Override
    protected RuleLogicEntity<ChatProcessAggregate> doCheckLogic(ChatProcessAggregate chatProcess, UserAccountEntity data, String... logics) throws ExecutionException {
        Map<String, ILogicFilter> logicFilter = logicFactory.getLogicFilter();
        RuleLogicEntity<ChatProcessAggregate> entity = null;
        for (String logic : logics) {
            if (DefaultLogicFactory.LogicModel.NULL.equals(logic)) continue;
            entity = logicFilter.get(logic).filter(chatProcess, data);
            if (!LogicCheckTypeVO.SUCCESS.equals(entity.getType())) return entity;
        }
        return entity != null ? entity :
                RuleLogicEntity.<ChatProcessAggregate>builder()
                        .type(LogicCheckTypeVO.SUCCESS)
                        .data(chatProcess)
                        .build();
    }
    
}
